/** ClientManager */
package client;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Logger;

import javax.swing.JFrame;

import client.events.AddAppointment;
import client.events.PatientEdit;
import client.events.PatientSearch;
import client.events.ViewAppointments;
import client.events.ViewHistory;
import client.events.ViewHome;
import client.events.ViewInvoice;
import client.events.ViewPatientInvoice;
import client.events.ViewTR;
import client.io.ClientIO;
import client.ui.panel.AddAppointmentPanel;
import client.ui.panel.AppointmentPanel;
import client.ui.panel.DoctorInvoicePanel;
import client.ui.panel.HistoryPanel;
import client.ui.panel.HomePanel;
import client.ui.panel.LoginPanel;
import client.ui.panel.PatientInvoicePanel;
import client.ui.panel.PatientPanel;
import client.ui.panel.ResultsPanel;
import client.ui.panel.TRPanel;
import domain.session.UserSession;

/** Handles actions by client. Passes actions to the event bus
 * @author jessienewman
 * @version $Revision: 1.0 $

 */
public class ClientManager {
	/**
	 * Field LOGGER.
	 */
	private static final Logger LOGGER = Logger.getLogger(ClientManager.class.getName());

	/**
	 * Field home.
	 */
	private static HomePanel Home;
	
	/**
	 * Field login.
	 */
	private static LoginPanel Login;
	
	/**
	 * Field patientEdit.
	 */
	private static PatientPanel PatientEdit;
	
	/**
	 * Field patientSearchResults.
	 */
	private static ResultsPanel PatientSearchResults;
	
	/**
	 * Field appointments.
	 */
	private static AppointmentPanel Appointments;
	
	/**
	 * Field treatmentRecord.
	 */
	private static TRPanel TreatmentRecord;
	
	/**
	 * Field history.
	 */
	private static HistoryPanel History;
	
	/**
	 * Field patientInvoice.
	 */
	private static PatientInvoicePanel PatientInvoice;
	
	/**
	 * Field doctorInvoice.
	 */
	private static DoctorInvoicePanel DoctorInvoice;
	
	/**
	 * Field addAppointment.
	 */
	private static AddAppointmentPanel AddAppointment;
	
	/**
	 * Field PORT.
	 */
	private static final Integer PORT = 12345;
	
	/**
	 * Field HOST.
	 * (value is ""localhost"")
	 */
	private static final String HOST = "localhost";
	
	/**
	 * Field SESSION.
	 */
	public static UserSession SESSION = new UserSession(0, 0);
	
	/**
	 * Field width.
	 */
	private static final Integer WIDTH = 1000;
	
	/**
	 * Field height.
	 */
	private static final Integer HEIGHT = 700;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final ClientManager manager = new ClientManager(); // $codepro.audit.disable variableUsage
		manager.show();
		manager.connect();
	}
	
	/**
	 * connects to the server
	 */
	private void connect() {
		try {
			ClientIO.setSocket(new Socket(HOST, PORT));
		} catch (final UnknownHostException e) {
			LOGGER.warning(e.getMessage());
		} catch (final IOException e) {
			LOGGER.warning(e.getMessage());
		}
	}
	
	/**
	 * shows the GUI as a cardLayout
	 */
	private static void show() {
		final JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
		frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		
		final CardLayout cardLayout = new CardLayout();
		frame.setLayout(cardLayout);
		
		Login = new LoginPanel(cardLayout);
		frame.add(Login, "Login");
		cardLayout.addLayoutComponent(Login, "Login");

		Home = new HomePanel();
		frame.add(Home, "Home");
		cardLayout.addLayoutComponent(Home, "Home");

		PatientEdit = new PatientPanel();
		frame.add(PatientEdit, "PatientEdit");
		cardLayout.addLayoutComponent(PatientEdit, "PatientEdit");

		PatientSearchResults = new ResultsPanel();
		frame.add(PatientSearchResults, "PatientSearchResults");
		cardLayout.addLayoutComponent(PatientSearchResults, "PatientSearchResults");

		Appointments = new AppointmentPanel();
		frame.add(Appointments, "Appointments");
		cardLayout.addLayoutComponent(Appointments, "Appointments");
		
		TreatmentRecord = new TRPanel();
		frame.add(TreatmentRecord, "TR");
		cardLayout.addLayoutComponent(TreatmentRecord, "TR");
		
		History = new HistoryPanel();
		frame.add(History, "Hisory");
		cardLayout.addLayoutComponent(History, "History");
		
		PatientInvoice = new PatientInvoicePanel();
		frame.add(PatientInvoice, "PatientInvoice");
		cardLayout.addLayoutComponent(PatientInvoice, "PatientInvoice");
		
		DoctorInvoice = new DoctorInvoicePanel();
		frame.add(DoctorInvoice, "DoctorInvoice");
		cardLayout.addLayoutComponent(DoctorInvoice, "DoctorInvoice");
		
		AddAppointment = new AddAppointmentPanel();
		frame.add(AddAppointment, "AddAppointment");
		cardLayout.addLayoutComponent(AddAppointment, "AddAppointment");
		
		final EventBus bus = EventBus.BUS;
		bus.addHandler(ViewHome.class, new Handler<ViewHome>() {
			@Override
			public void handle(ViewHome event) {
				final Integer id = ClientManager.SESSION.getUserId();
				Home.updateUser(id);
				PatientEdit.updateUser(id);
				PatientSearchResults.updateUser(id);
				Appointments.updateUser(id);
				TreatmentRecord.updateUser(id);
				History.updateUser(id);
				Home.addUser(id);
				PatientInvoice.updateUser(id);
				DoctorInvoice.updateUser(id);
				AddAppointment.updateUser(id);
				
				//patientInvoice.updateAppointment(null);
				cardLayout.show(Home.getParent(), "Home");

			}
			
		});
		bus.addHandler(PatientEdit.class, new Handler<PatientEdit>() {
			@Override
			public void handle(PatientEdit event) {
				cardLayout.show(PatientEdit.getParent(), "PatientEdit");
				PatientEdit.addUser(event.patient);
			}
		});
		bus.addHandler(PatientSearch.class, new Handler<PatientSearch>() {
			@Override
			public void handle(PatientSearch event) {
				cardLayout.show(PatientSearchResults.getParent(), "PatientSearchResults");
				PatientSearchResults.addResults(event.resultsList);
			}
		});
		bus.addHandler(ViewAppointments.class, new Handler<ViewAppointments>() {
			@Override
			public void handle(ViewAppointments event) {
				cardLayout.show(Appointments.getParent(),  "Appointments");
				Appointments.addAppointments(event.appointments, event.user);
			}
		});
		bus.addHandler(ViewTR.class, new Handler<ViewTR>() {
			@Override
			public void handle(ViewTR event) {
				cardLayout.show(TreatmentRecord.getParent(), "TR");
				TreatmentRecord.addTreatmentRecord(event.record);
			}
		});
		bus.addHandler(ViewHistory.class, new Handler<ViewHistory>() {
			@Override
			public void handle(ViewHistory event) {
				cardLayout.show(History.getParent(), "History");
				History.addPatient(event.patient);
			}
		});
		bus.addHandler(ViewPatientInvoice.class, new Handler<ViewPatientInvoice>() {
			@Override
			public void handle(ViewPatientInvoice event) {
				cardLayout.show(PatientInvoice.getParent(), "PatientInvoice");
				PatientInvoice.updateAppointment(event.appointment);
			}
		});
		bus.addHandler(ViewInvoice.class, new Handler<ViewInvoice>() {
			@Override
			public void handle(ViewInvoice event) {
				cardLayout.show(DoctorInvoice.getParent(), "DoctorInvoice");
				DoctorInvoice.updateDoctor(null);
			}
		});
		bus.addHandler(AddAppointment.class, new Handler<AddAppointment>() {
			@Override
			public void handle(AddAppointment event) {
				cardLayout.show(AddAppointment.getParent(), "AddAppointment");
				AddAppointment.addUser(event.user);
			}
		});
		
		frame.pack();
		frame.setVisible(true);
		LOGGER.info("Panel created");
	}
	
	/**
	 * returns the super's toString
	
	 * @return String super's toString */
	public String toString() {
		return super.toString();
	}

}